package actions;

//~--- non-JDK imports --------------------------------------------------------

import beans.AdminForm;
import beans.memberformbean;

import dto.JfMembers;
import dto.Sections;
import dto.Topics;

import model.AdminControlPanel;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.actions.DispatchAction;

//~--- JDK imports ------------------------------------------------------------

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class AdminAction extends DispatchAction {
//	 AdminControlPanel admin ;
//	    Vector<JfMembers> members = new Vector<JfMembers>();
//	    Vector<JfMembers> moderator = new Vector<JfMembers>();
//	    Vector<Sections> sections = new Vector<Sections>();
//	    String memid;
    public AdminAction() {

        // TODO Auto-generated constructor stub
    }

    public ActionForward open(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response)
            throws Exception {
        HttpSession session = request.getSession(false);

        session.setAttribute("forum", "true");

        ActionForward forward = new ActionForward();
        int id=((AdminForm) form).getSectionid();
 AdminControlPanel acp = new AdminControlPanel();
 Sections newParent=null;
        System.out.println(id);
        if(id!=0){             
        acp.startNew();
         newParent = acp.getSection(id);

        }
       
        display(id, acp, request);
        session.setAttribute("parent", newParent);
        forward = mapping.findForward("sections");
        return forward;
    }

    public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                HttpServletResponse response)
            throws Exception {
        HttpSession   session = request.getSession(false);
        ActionForward forward = new ActionForward();
        int           id      = ((AdminForm) form).getSectionid();

        System.out.println(id);

        AdminControlPanel acp = new AdminControlPanel();

        acp.startNew();
        acp.deleteSection(id);

        Sections parent   = (Sections) session.getAttribute("parent");
        int      parentid = 0;

        if (parent != null) {
            parentid = parent.getSectionid().intValue();
        }

        System.out.println(parentid);

        boolean check = acp.save();

        display(parentid, acp, request);

        if (!check) {
            ActionErrors errors = new ActionErrors();

            errors.add("DeleteSectionError", new ActionMessage("DeleteError"));
            saveErrors(request, errors);
        }

        forward = mapping.findForward("sections");

        return forward;
    }

    public ActionForward createSection(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                       HttpServletResponse response)
            throws Exception {
        HttpSession       session  = request.getSession(false);
        ActionForward     forward  = new ActionForward();
        int               isPublic = ((AdminForm) form).getIspublic();
        String            name     = ((AdminForm) form).getNewSectionName();
        AdminControlPanel acp      = new AdminControlPanel();

        acp.startNew();

        Sections parent = (Sections) session.getAttribute("parent");

        acp.createSection(name, parent, isPublic);

        int parentid = 0;

        if (parent != null) {
            parentid = parent.getSectionid().intValue();
            System.out.println("/////////////// got parent id");
        }

        System.out.println(parentid);

        boolean check = acp.save();
        if (!check) {
            System.out.println("/////////////// what's wrong");
            ActionErrors errors = new ActionErrors();
            errors.add("CreateSectionError", new ActionMessage("DuplicateName"));
            saveErrors(request, errors);
        }

         display(parentid, acp, request);
                ((AdminForm) form).setIspublic(0);
                ((AdminForm) form).setNewSectionName("");
        forward = mapping.findForward("sections");

        return forward;
    }

    public ActionForward setSectionPublicity(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response)
            throws Exception {
        HttpSession       session  = request.getSession(false);
        ActionForward     forward  = new ActionForward();
        int               isPublic = ((AdminForm) form).getIspublic();
        int               id       = ((AdminForm) form).getSectionid();
        AdminControlPanel acp      = new AdminControlPanel();

        acp.startNew();

        Sections parent = (Sections) session.getAttribute("parent");

        acp.setSectionPublicity(id, isPublic);

        int parentid = 0;

        if (parent != null) {
            parentid = parent.getSectionid().intValue();
        }

        System.out.println(parentid);

        boolean check = acp.save();

        display(parentid, acp, request);

        if (!check) {
            ActionErrors errors = new ActionErrors();

            errors.add("SetPublicityError", new ActionMessage("ErrorPublicity"));
            saveErrors(request, errors);
        }
        ((AdminForm) form).setIspublic(0);
        forward = mapping.findForward("sections");

        return forward;
    }

    public ActionForward setTopicPublicity(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response)
            throws Exception {
        HttpSession       session  = request.getSession(false);
        ActionForward     forward  = new ActionForward();
        int               isPublic = ((AdminForm) form).getIspublic();
        int               id       = ((AdminForm) form).getTopicid();
        AdminControlPanel acp      = new AdminControlPanel();

        acp.startNew();

        Sections parent = (Sections) session.getAttribute("parent");

        acp.setTopicPublicity(id, isPublic);

        int parentid = 0;

        if (parent != null) {
            parentid = parent.getSectionid().intValue();
        }

        System.out.println(parentid);

        boolean check = acp.save();

        display(parentid, acp, request);

        if (!check) {
            ActionErrors errors = new ActionErrors();

            errors.add("SetPublicityError", new ActionMessage("ErrorPublicity"));
            saveErrors(request, errors);
        }
        ((AdminForm) form).setIspublic(0);
        forward = mapping.findForward("sections");

        return forward;
    }
    
    public ActionForward getTree(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response)
    throws Exception {
    	ActionForward     forward  = new ActionForward();
    	AdminControlPanel acp      = new AdminControlPanel();
        acp.startNew();
        Vector<Sections> tree=acp.getSectionsTree();
        request.setAttribute("tree", tree);
        request.setAttribute("sections", null);
        acp.finish();
        forward = mapping.findForward("sections");

        return forward;
	}

    public void display(int id, AdminControlPanel acp, HttpServletRequest request) {
        List<Sections> sections = null;
        List<Topics> topics   = null;
        Vector<Sections> parents=null;
        request.setAttribute("tree", null);
        request.setAttribute("sections", null);
        request.setAttribute("topics", null);
        acp.startNew();

        if (id == 0) {
            sections = acp.getForums();
        } else {
            Sections section = acp.getSection(id);
            parents =acp.getParentTree(section);
            sections = new ArrayList<Sections>(section.getSectionses());
            topics   = new ArrayList<Topics>(section.getTopicses());
            if(topics.size()==0){
            	topics=null;
            }
            if(parents.size()==0){
            	parents=null;
            }
            
            
        }        
        request.setAttribute("sections", sections);
        request.setAttribute("topics", topics);
              request.setAttribute("tree", parents);
        acp.finish();
    }
    
    
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////
    /**
     * user actions
     */
    }

